// app.js
import { get_user_info } from './api/user'
// import EventEmitter from './utils/event' // 事件总线
import promisePolyfill from './utils/promise-polyfill'
import { getToken, autoLogin } from './utils/permission'
App({
  // 引入`towxml3.0`解析方法
  towxml: require('/towxml/index'),
  // event: new EventEmitter(),
  onLaunch() {
    // 自动登陆
    autoLogin()
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 获取系统信息
    wx.getSystemInfo({
      success: (result) => {
        console.log('系统信息', result)
        this.globalData.systemInfo = result
        this.globalData.moveIcon = {
          X: result.windowWidth * 5 / 6,
          Y: result.windowHeight * 3 / 4,
        }
        this.globalData.StatusBar = result.statusBarHeight;
        let capsule = wx.getMenuButtonBoundingClientRect();
        if (capsule) {
          this.globalData.Custom = capsule;
          this.globalData.CustomBar = capsule.bottom + capsule.top - result.statusBarHeight;
        } else {
          this.globalData.CustomBar = result.statusBarHeight + 50;
        }

        // console.log(this.event)

      },
    })

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  globalData: {
    userInfo: null,
    systemInfo: null,
    // 移动按钮位置
    moveIcon: {
      X: 0,
      Y: 0,
    },
    ColorList: [{
      title: '嫣红',
      name: 'red',
      color: '#e54d42'
    },
    {
      title: '桔橙',
      name: 'orange',
      color: '#f37b1d'
    },
    {
      title: '明黄',
      name: 'yellow',
      color: '#fbbd08'
    },
    {
      title: '橄榄',
      name: 'olive',
      color: '#8dc63f'
    },
    {
      title: '森绿',
      name: 'green',
      color: '#39b54a'
    },
    {
      title: '天青',
      name: 'cyan',
      color: '#1cbbb4'
    },
    {
      title: '海蓝',
      name: 'blue',
      color: '#0081ff'
    },
    {
      title: '姹紫',
      name: 'purple',
      color: '#6739b6'
    },
    {
      title: '木槿',
      name: 'mauve',
      color: '#9c26b0'
    },
    {
      title: '桃粉',
      name: 'pink',
      color: '#e03997'
    },
    {
      title: '棕褐',
      name: 'brown',
      color: '#a5673f'
    },
    {
      title: '玄灰',
      name: 'grey',
      color: '#8799a3'
    },
    {
      title: '草灰',
      name: 'gray',
      color: '#aaaaaa'
    },
    {
      title: '墨黑',
      name: 'black',
      color: '#333333'
    },
    {
      title: '雅白',
      name: 'white',
      color: '#ffffff'
    },
    ],
    icon: [{ name: 'appreciate', isShow: true }, { name: 'check', isShow: true }, { name: 'close', isShow: true }, { name: 'edit', isShow: true }, { name: 'emoji', isShow: true }, { name: 'favorfill', isShow: true }, { name: 'favor', isShow: true }, { name: 'loading', isShow: true }, { name: 'locationfill', isShow: true }, { name: 'location', isShow: true }, { name: 'phone', isShow: true }, { name: 'roundcheckfill', isShow: true }, { name: 'roundcheck', isShow: true }, { name: 'roundclosefill', isShow: true }, { name: 'roundclose', isShow: true }]

  },
  getUserInfo: function (reset = false) {
    if (!getToken()) {
      return Promise.reject(new Error('未登陆'))
    }
    console.log('resetreset', reset)
    // 已经存在用户信息，直接返回
    if (USER_INFO.id) {
      if (!reset) {
        return Promise.resolve(USER_INFO)
      }
    }
    // 不存在用户信息，调用获取
    return get_user_info().then(res => {
      this.setUserInfo(res.data)
      return res.data
    })
  },
  /**
   * 设置用户信息
   * @param {} userInfo 
   */
  setUserInfo: function (userInfo) {
    USER_INFO = Object.assign({}, USER_INFO, userInfo)
    // this.event.emit('userInfoChanged', { userInfo: userInfo })
  }
})
var USER_INFO = {
  id: null,
  nickname: '',
  avatar_url: '',
}
